package org.apogee.mil.client;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.portal.PortalForm;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.client.widgets.DialogHelper;
import org.apogee.mil.client.widgets.ServerConfig;

import com.extjs.gxt.themes.client.Slate;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
import com.google.gwt.user.client.Window.ClosingHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CDCWebApp implements EntryPoint {
	private int id = -1;
	public void onModuleLoad() {

		ThemeManager.register(Slate.SLATE); //register non standard theme
		Slate.SLATE.set("file","gxt/themes/slate/css/xtheme-slate.css"); //set custom path for SLATE theme
		GXT.setDefaultTheme(Slate.SLATE, true); //set default theme to new SLATE skin

		id = ServerServiceFactory.createServerService();
		Session.setSession(id);


		final UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {

			@Override
			public void onUncaughtException(Throwable e) {


			}
		};

		GWT.setUncaughtExceptionHandler(handler);

		ServerServiceFactory.getGreetService(id).loadConfig(new AsyncCallback<Boolean>() {

			@Override
			public void onSuccess(Boolean result) {
				if (result == false) {
					DialogHelper dh = new DialogHelper("", new ServerConfig());
					dh.show();
					return;
				}

				boolean results = ServerServiceFactory.initializeFactory(id);

				if (results == false) {
					MessageBox.alert("Error Initializing", "Unable to Load Configuration", null);
					return;
				}
				try {
					load();
				} catch (Exception e) {
					handler.onUncaughtException(e);
					
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("Error", "An exception was thrown:\n" + caught.getMessage(), null);
			}
		});


	}

	private void load() {
		final ContentPanel cp = new ContentPanel();		
		cp.setSize(Window.getClientWidth(), Window.getClientHeight());
		cp.getHeader().setVisible(false);
		cp.setBodyStyleName("backgroundImage");
		cp.setBodyBorder(false);

		ContentPanel title = new ContentPanel();
		title.getHeader().setVisible(false);
		title.setBodyBorder(false);
		title.setHeight(200);
		title.setBodyStyleName("headImage");

		cp.add(title);

		cp.add(new PortalForm());

		RootPanel.get("form").add(cp);
		RootPanel.getBodyElement().getStyle().setBackgroundColor("#5d8aa8");

		Window.addWindowClosingHandler(new ClosingHandler() {

			@Override
			public void onWindowClosing(ClosingEvent event) {
				ServerServiceFactory.close(id);
				GWT.log("Closing");

			}
		});

		Window.addResizeHandler(new ResizeHandler() {

			@Override
			public void onResize(ResizeEvent event) {
				cp.setSize(event.getWidth(), event.getHeight());

				cp.layout(true);

			}
		});

		Window.addWindowClosingHandler(new ClosingHandler() {

			@Override
			public void onWindowClosing(ClosingEvent event) {
				ServerServiceFactory.close(id);

			}
		});

	}
}
